package com.serverapk.camera;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import android.util.Log;

import com.serverapk.BaseApplication;
import com.serverapk.PostNex;
import com.serverapk.interfaces.OkHttpCallbackListener;
import com.serverapk.para.Common;
import com.serverapk.util.L;

public class ImgDeal {
    private String TAG = "ImgDeal";
    private CameraService cameraService = new CameraService();

    private List<String> mResponse;

    public void getImgList() {
        L.e("getImgList======11111111111111111");
        boolean fileFlag = true;
        List<String> imgList = new ArrayList<String>();
        for (int i = 0; i < Common.socketMap.size(); i++) {
            String img_path = Common.filepath + i + ".jpg";
            L.e("img_path1==" + img_path);
            File file = new File(img_path);
            if (!file.exists()) {
                L.e("img_path==" + img_path);
                fileFlag = false;
                imgList = null;
                break;
            }
        }
        L.e("getImgList======" + fileFlag);
        if (fileFlag) {
            for (int i = 0; i < Common.socketMap.size(); i++) {
                String img_path = Common.filepath + i + ".jpg";
                L.e("img_path2==" + img_path);
                imgList.add(img_path);
            }
//            if (BaseApplication.getInstance().flag) {
//                L.e("flag=true==========================");
//                PostNex.notifyPost(mResponse, imgList);
//            } else {

            // 发送图片至服务器
            cameraService.sendPicture(imgList, new OkHttpCallbackListener() {

                @Override
                public void onFinish(List<String> response) {
                    // 算法服务器返回结果response
                    // 结果上报后台服务器Common.nex_url
                    mResponse = response;
                    L.e("识别结果：" + response);
                    PostNex.notifyPost(response,"");

                    ImgChange.changImg();
                    //TODO 注释掉 因为 不注释 有可能会导致notifyPost耗时长，现在改为notifyPost成功后再imagePost
//                    PostNex.imagePost();
                }

                @Override
                public void onError(int code, String e) {
                    //上送算法服务器异常
                    L.e("onError11：" + e.toString());
                    PostNex.errorPost(code, e);
                }
            });
//            }
        } else {
            //获取图片异常
            PostNex.errorPost(Common.error_code3, Common.error_info3);
        }
    }

}
